package com.github.sd4324530.fastweixin.api.extra;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 使用listener 定义
 */
public class ListenerSupport implements ServletContextListener{
    private ScheduledExecutorService service;

    public final static TokenCache CACHE = new ConcurrentHashMapTokenCache();

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        service = Executors.newScheduledThreadPool(5);
        service.scheduleWithFixedDelay(new TokenRefreshTask(CACHE), 0, TokenRefreshTask.PERIOD, TimeUnit.MILLISECONDS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        service.shutdown();
        service = null;
    }
}
